﻿Imports microsoft.Data.SQLite

Public Class 修改密码
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then MsgBox("内容不能为空！") : Exit Sub
        If TextBox2.Text <> TextBox3.Text Then MsgBox("两次输入的密码不一致") : Exit Sub
        Using connection As New sqliteconnection(_connString)
            Try
                connection.Open()
                ' 检查旧密码是否正确 
                Dim checkSql As String = "SELECT 密码 FROM userlist WHERE 账号 = @account"
                Using checkCmd As New sqliteCOMMAND(checkSql, connection)
                    checkCmd.Parameters.AddWithValue("@account", username)
                    Dim reader As sqliteDataReader = checkCmd.ExecuteReader()
                    If reader.HasRows Then
                        ' 检查是否和原密码一致
                        reader.Read()
                        Dim oldpassword As String = DecryptString(reader.GetString(reader.GetOrdinal("密码")))

                        If TextBox1.Text.Trim() <> oldpassword Then
                            MsgBox("原密码不正确，请检查", vbOKOnly, "提示")
                            Exit Sub
                        End If

                        If TextBox2.Text.Trim() = oldpassword Then
                            MsgBox("新密码和原密码一致，请检查", vbOKOnly, "提示")
                            Exit Sub
                        End If
                        reader.Close()
                        '修改密码
                        Dim updateSql As String = "UPDATE userlist SET 密码 = @password WHERE 账号 = @account"
                        Dim updateCmd As New sqliteCOMMAND(updateSql, connection)
                        updateCmd.Parameters.AddWithValue("@account", username)
                        updateCmd.Parameters.AddWithValue("@password", EncryptString(TextBox2.Text))
                        updateCmd.ExecuteNonQuery()
                        MsgBox("修改成功！", vbOKOnly, "提示")
                    End If
                End Using

            Catch ex As Exception
                MessageBox.Show($"操作失败: {ex.Message}")
            End Try
        End Using
    End Sub

    Private Sub 修改密码_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GroupBox1.Left = Me.Left
        GroupBox1.Top = Me.Top
    End Sub
End Class